<script type="text/javascript">
var enable_submit=true;
function submit_pool_create(){ 
    var bValid = true;
    var mark=$("#pool_mark_cr");
    var protocol = $("#pool_create_modal").find("#protocol_type");
    var protection_domain = $("#pool_create_modal").find(".pool_PDs");
    var repnum = $("#pool_create_modal").find("#repnum");
    var quota = $("#pool_create_modal").find("#quota");
    var target_tier = $("#pool_create_modal").find("#pool_tt_select");
    var delete_code = $("#pool_create_modal").find("#delete_code");
    var delete_code_r = $("#pool_create_modal").find("#delete_code_r");
    var chap_name = $("#pool_create_modal").find("#chap_name");
    var chap_password= $('#pool_create_modal').find("#chap_password");
    var provisioning = $('#pool_create_modal').find("input[name='disk_preparation']:checked").val();
    bValid = bValid && checkLength(mark, "名称", 1, 32 );    
    bValid = bValid && checkRegexp( mark, /^[0-9a-z-]+$/, "名称应由小写英文字母、数字或中划线(英文)构成。");
    if(quota.val()) {
        bValid = bValid && checkRegexp_num(quota, /^[0-9]+$/i, "配额必须为大于0的整数。");
    }
    if($.trim(chap_name.val()).length>0||$.trim(chap_password.val()).length>0) {
         bValid = bValid && checkCHAP(chap_name, chap_password);
    }
    if($(".repnum").is(":hidden")){
        repnum.attr("realvalue","");
    }
    if($(".code_delete_op").is(":hidden")){
        delete_code.attr("realvalue","");
        delete_code_r.attr("realvalue","");
    }
    PD_ids = _multi_checkbox_get_ids(protection_domain);
    //用两个布尔值来防止表单的重复提交
    if(bValid){
        if(enable_submit) {
            enable_submit = false;
            var $submitBtn = $('.modal-footer').find('.btn-blue');
            $submitBtn.html('<i class="fa fa-spinner fa-spin" style="margin:3px 6px;"></i>');
            $('.rtn-tip').slideUp('fast');
            $.post("/pool/create",{
                "quota": $.trim(quota.val()),
                "name": $.trim(mark.val()),
                "protocol": $.trim(protocol.attr("realvalue")),
                "protection_domain_ids": PD_ids,
                "repnum": $.trim(repnum.attr("realvalue")),
                "priority": $.trim(target_tier.attr("realvalue")),
                'chap_name': $.trim(chap_name.val()),
                'chap_password': $.trim(chap_password.val()),
                "ec_code": $.trim(delete_code_r.attr("realvalue")),
                "ec_data": $.trim(delete_code.attr("realvalue")),
                "provisioning": provisioning
            },function(data){
                if (data.reply.is_success){
                    angular.element('#pool_create_button').scope().close();
                    $('#pool_available_tb').trigger("reloadGrid");
                    enable_submit = true;
                }else{
                    var error = data.reply.error;
                    $('.rtn-tip').text(error).slideDown('fast');
                    $submitBtn.html(SelfTranslate("[[ 'label ok'|translate ]]"));
                    enable_submit = true;
                }
            });
        }
    }
}
    function protocol_type_click(a, b){
        if(b == "nbd" || b == "nfs" )
        {
             $(".user-info").hide();
        }
        if(b == "iscsi")
        {
             $(".user-info").show();
        }
     }
$(function() {
    /* 初始化下拉列表 */
    var protocol_mode = [];
    {% for protocol in protocols %}
        var values = new Array();
        var protocol = "{{ protocol }}".toUpperCase();
        if (protocol == 'ISCSI'){
            protocol = "iSCSI";
        }
        values[0] = "{{ protocol }}";
        values[1] = protocol;
        protocol_mode.push(values)
    {% endfor %}
    
    $('#protocol_type').rqDropDown({
        "datatype":"local",
        "source":protocol_mode, 
        "onchange": "protocol_type_click",
        position:true
    }); 
     
    $('#pool_create_modal #repnum').rqDropDown({
        "datatype":"local",
        "source":get_repnums(),
        position:true
    }); 
    $('#pool_create_modal #pool_tt_select').rqDropDown({
        "datatype":"local",
        "source":get_tiers(),
        position:true
    });
    
    $('#pool_create_modal #delete_code').rqDropDown({
        "datatype":"local",
        "source":delete_code
    });
    var delete_code_r = [['1', '1'],['2', '2'],['3', '3'],['4', '4']];
    $('#pool_create_modal #delete_code_r').rqDropDown({
        "datatype":"local",
        "source":delete_code_r
    });
    if(val_payPlatform == "on"){
        var val_payPlatform = $('.pool_rep_rep[name="rep"]:checked ').val();
        $("#delete_code").removeAttr("id");
    }

    var repnum = $("#pool_create_modal").find("#repnum");
    $(":radio").click(function() {
        if (($(this)).hasClass("pool_rep_rep") == true) {
            $(".repnum").show();
            $(".code_delete_op").hide();
        }
        if (($(this)).hasClass("pool_rep_delete") == true) {
            $(".code_delete_op").show();
            $(".repnum").hide();
        }
    });
     $(":radio").click(function() {
        if (($(this)).hasClass("pool_rep_rep") == true) {
            $(".repnum").show();
            $(".code_delete_op").hide();
        }
        if (($(this)).hasClass("pool_rep_delete") == true) {
            $(".code_delete_op").show();
            $(".repnum").hide();
        }
    });
    /* 初始化下拉列表结束 */
    /* 为小图标绑定鼠标移入移出事件 */
     var delete_code_l = [['1', '1'],['2', '2'],['3', '3'],['4', '4']];
     $('#pool_create_modal #delete_code').rqDropDown({
    "datatype":"local",
    "source":delete_code_l
     }); 
    var obj = {
        'pool_name':'由1至32位小写英文字母、数字、中划线(英文)构成。',
        'quota': '不填写，则配额不受限，若填写，须为大于0的整数。',
        'chap_password': '可为空, 为空时将使用所属资源池的CHAP用户名称;由3至32位小写英文字母、数字、中划线构成。',
        'chap_name': '可为空, 为空时将使用所属资源池的CHAP用户名称;如果资源池未设置CHAP用户，则使用所属用户的CHAP用户;由3至32位小写英文字母、数字、中划线构成'
    }
    bindMouseEvent('#pool_create_modal', obj);
    /* 为小图标绑定鼠标移入移出事件结束 */
});
     
</script>
<style>
    .code_delete_op{
        display: none;
    }
</style>
<div class="modal-header">  
    <h3>创建资源池</h3>  
</div> 
<div class="modal-body modal-body-height" id="pool_create_modal">

<div class="pool-create-box">
<div class="rtn-tip"></div>
<table class="base-info">
    <tr>
        <td>
            <label>名称</label><a>*</a>
        </td>
        <td colspan="3">
            <input id="pool_mark_cr" name="pool_mark_cr" size="25" max="10000" min="1" type="text"/>
            <i class="fa fa-question-circle" itype="pool_name"></i>
        </td>
    </tr>
    <tr>
        <td>
            <label>[['label quota'|translate]]</label>
        </td>
        <td>
            <input id="quota" name="quota" max="10000" min="1" type="text" />
            <span class="unit">G</span>
            <i class="fa fa-question-circle" itype="quota"></i>
        </td>
        <td>
            <label>协议类型</label><a>*</a>
        </td>
        <td>
            <input id="protocol_type" readonly="readonly" class="rounded_dropdown" value='iSCSI' realvalue="iscsi"/>
        </td>
    </tr>
</table>

<table class="super-info">
    <tr>
        <td>
            <label>保护域</label>
        </td>
        <td>
            {% if protection_domains|length ==0%}
            <a>节点未分配保护域</a>
            {% endif %}
            {% for pd in protection_domains %}
            <label><input type="checkbox" class="pool_PDs" value="{{pd.id}}">{{pd.name}}</label>
            {% endfor %}
        </td>
        <td style="display:none">
            <label for="protocol_type">保护策略:</label>
        </td>
        <td style="display:none">
            <label><input type="radio" checked="checked" name="rep" class="pool_rep_rep">[['replication number'|translate]]</label>
            <label><input type="radio" name="rep" class="pool_rep_delete">纠删码</label>
        </td> 
    </tr>
    <tr>
        <td>
            <label>[['Disk configuration'|translate]]</label><a>*</a>
        </td>
        <td>
            <label><input  type="radio" name="disk_preparation" value="thin" style="vertical-align:-3px;" checked="true"/>[['modal complex'|translate]]</label>
            <label><input  type="radio" name="disk_preparation" value="thick" style="vertical-align:-3px;" />[['modal streamline'|translate]]</label>
        </td>
    </tr>
    <tr>
        <td class="repnum">
            <label>[['replication number'|translate]]</label>
        </td>
        <td class="repnum"><input id="repnum" readonly="readonly" class="rounded_dropdown" value='{{user.sp_repnum}}' realvalue="{{user.sp_repnum}}"/></td>

        <td class="code_delete_op">
            <label>[['replication number'|translate]]</label>
        </td>
        <td class="code_delete_op">
                <input id="delete_code" style="width:40%;" readonly="readonly" class="rounded_dropdown" value='1' realvalue="1"/>
            <b>+</b>
                <input  id="delete_code_r" style="width:40%;" readonly="readonly" class="rounded_dropdown" value='1' realvalue="1"/>

        </td>


        <td>
            <label>目标分层</label>
        </td>
        <td>
            <input id="pool_tt_select" readonly="readonly" class="rounded_dropdown" value='自动分层' realvalue="-1"/>
        </td>

    </tr>
</table>
<table class="user-info">
    <tr>
        <td>
            <label>[["label chap name"|translate]]</label>
        </td>
        <td>
            <input type="text" name="chap_name" id="chap_name"  />
            <i class="fa fa-question-circle" itype="chap_name"></i>
        </td>
        <td>
            <label>[["label chap password"|translate]]</label>
        </td>
        <td>
            <input type="password" name="chap_password" id="chap_password" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onpaste="return false" oncontextmenu="window.event.returnValue=false">
            <i class="fa fa-eye" style="right:60px"></i>
            <i class="fa fa-question-circle" itype="chap_password"></i>
        </td>
    </tr>
</table>
</div>
<div class="error-tip">
    <p></p><span></span>
</div>
<div class="tip">
    <p></p><span></span>
</div>
</div> 
<div class="modal-footer">  
    <button class="btn btn-blue" ng-click="ok()">[[ 'label ok'|translate ]]</button>  
    <button class="btn btn-white" ng-click="cancel()">[[ 'label cancel'|translate ]]</button>  
</div>
